package com.yq.cp.controller.basics;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yq.cp.entity.basics.TotalSettlement;
import com.yq.cp.entity.common.ApiResult;
import com.yq.cp.entity.common.PageVo;
import com.yq.cp.service.basics.TotalSettlementService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

/**
 * <p>
 * 结算总表 前端控制器
 * </p>
 *
 * @author shenyuqing
 * @since 2024-08-27
 */
@RestController
@RequestMapping("/totalSettlement")
public class TotalSettlementController {
    @Resource
    TotalSettlementService totalSettlementService;
    /**
     * 详情
     * @param totalSettlement
     * @return
     */
    @PostMapping("details")
    public ApiResult detail(@RequestBody TotalSettlement totalSettlement){
        TotalSettlement one =  totalSettlementService.detail(totalSettlement);
        return ApiResult.success(one);
    }


    /**
     * 结算列表
     * @param pageVo
     * @return
     */
    @PostMapping("list")
    public ApiResult list(@RequestBody PageVo pageVo){
        IPage<TotalSettlement> page = totalSettlementService.selListUser(pageVo);
        return ApiResult.success(page);
    }

    /**
     * 微信支付
     * @param totalSettlement
     * @return
     */
    @PostMapping("wxFee")
    public ApiResult wxFee(@RequestBody TotalSettlement totalSettlement){
       Map map =  totalSettlementService.wxFee(totalSettlement);
        return ApiResult.success(map);
    }

    /**
     * 缴费记录
     * @param pageVo
     * @return
     */
    @PostMapping("record")
    public ApiResult record(@RequestBody PageVo pageVo){
        Page<TotalSettlement> page =  totalSettlementService.record(pageVo);
        return ApiResult.success(page);
    }

    /**
     * 详情改
     * @param totalSettlement
     * @return
     */
    @PostMapping("detail")
    public ApiResult details(@RequestBody TotalSettlement totalSettlement){
        TotalSettlement one =  totalSettlementService.details(totalSettlement);
        return ApiResult.success(one);
    }

    @PostMapping("/download")
    public void downloads(@RequestBody @Valid TotalSettlement totalSettlement, HttpServletResponse response, HttpServletRequest request) throws UnsupportedEncodingException {
        TotalSettlement byId = totalSettlementService.getById(totalSettlement.getId());
        // 防止日志记录获取session异常
        request.getSession();
        // 设置编码格式
        response.setContentType("application/pdf;charset=UTF-8");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode(byId.getYear()+"年"+byId.getMonth()+"月"+byId.getSiteName()+"结算清单", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".pdf");
        totalSettlementService.downloads(totalSettlement, response);
    }


}

